package shoppingmall.reception.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import shoppingmall.reception.dto.PageBean;
import shoppingmall.reception.dto.ProductClassificationIncludeProductInfoVO;
import shoppingmall.reception.dto.ProductVO;
import shoppingmall.reception.mapper.CommercialSpecificationMapper;
import shoppingmall.reception.mapper.ProductClassificationMapper;
import shoppingmall.reception.mapper.ProductMapper;
import shoppingmall.reception.pojo.CommercialSpecification;
import shoppingmall.reception.pojo.Product;
import shoppingmall.reception.pojo.ProductClassification;
import shoppingmall.reception.service.ProductClassificationService;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 商品分类服务层相关-接口实现类
 */
@Service
public class ProductClassificationServiceImpl implements ProductClassificationService {
    //商品规格mapper层实例
    @Autowired
    private CommercialSpecificationMapper commercialSpecificationMapper;
    //商品mapper层实例
    @Autowired
    private ProductMapper productMapper;

    //商品分类mapper层实例
    @Autowired
    private ProductClassificationMapper productClassificationMapper;

    /**
     * 查询所有的商品分类信息
     */
    @Override
    public PageBean getAllProductClassification() {
        //调用mapper层方法进行查询
        List<ProductClassification> allProductClassification = productClassificationMapper.getAllProductClassification();
        return new PageBean(allProductClassification.size(), allProductClassification);
    }

    /**
     * 查询所有当前分类下的商品信息
     */
    @Override
    public PageBean getAllProductClassificationIncludeProductInfo() {
        //调用mapper层方法查询所有的商品分类信息
        List<ProductClassification> allProductClassification = productClassificationMapper.getAllProductClassification();
        //查询所有的商品信息
        List<Product> allProductList = productMapper.getAllProductList();
        //查询所有的商品规格信息
        List<CommercialSpecification> allCommercialSpecification = commercialSpecificationMapper.getAllCommercialSpecification();

        //往商品信息中加入规格参数数据
        List<ProductVO> productVOS = new ArrayList<>();
        Iterator iterator = allProductList.iterator();
        while (iterator.hasNext()){
            Product product = (Product) iterator.next();
            //准备ProductVO实例
            ProductVO productVO = new ProductVO();
            productVO.setProductId(product.getCommodityId());
            productVO.setProductName(product.getCommodityName());
            productVO.setProductDescription(product.getCommodityDescription());
            productVO.setProductDiscount(product.getCommodityDiscount());
            productVO.setProductSales(product.getCommoditySales());
            productVO.setProductSaleMoney(product.getCommoditySaleMoney());
            productVO.setProductClassificationId(product.getCommodityCategoryId());
            productVO.setProductImgs(product.getCommodityImgs());
            productVO.setCreateTime(product.getCreateTime());
            productVO.setUpdateTime(product.getUpdateTime());
            //处理商品分类名称
            for (int i = 0; i < allProductClassification.size(); i++) {
                if(product.getCommodityCategoryId() == allProductClassification.get(i).getCategoryId()){
                    productVO.setProductClassificationName(allProductClassification.get(i).getCategoryName());
                }
            }
            //处理商品规格信息
            List<CommercialSpecification> commercialSpecificationList = new ArrayList<>();
            for (int j = 0; j < allCommercialSpecification.size(); j++) {
                if(product.getCommodityId() == allCommercialSpecification.get(j).getCommodityId()){
                    commercialSpecificationList.add(allCommercialSpecification.get(j));
                }
            }
            productVO.setCommercialSpecificationList(commercialSpecificationList);
            //加入进去
            productVOS.add(productVO);
        }

        //将当前分类下的商品加入进去
        List<ProductClassificationIncludeProductInfoVO> list = new ArrayList<>();
        for(ProductClassification productClassification : allProductClassification) {
            ProductClassificationIncludeProductInfoVO productClassificationIncludeProductInfoVO = new ProductClassificationIncludeProductInfoVO();
            productClassificationIncludeProductInfoVO.setCategoryId(productClassification.getCategoryId());
            productClassificationIncludeProductInfoVO.setCategoryName(productClassification.getCategoryName());
            productClassificationIncludeProductInfoVO.setUpdateTime(productClassification.getUpdateTime());
            productClassificationIncludeProductInfoVO.setCreateTime(productClassification.getCreateTime());
            //处理当前分类下的所有商品信息
            List<ProductVO> currentClassificationCommercialSpecification = new ArrayList<>();
            for (int k = 0; k < productVOS.size(); k ++) {
                if(productClassification.getCategoryId() == productVOS.get(k).getProductClassificationId()){
                    currentClassificationCommercialSpecification.add(productVOS.get(k));
                }
            }
            productClassificationIncludeProductInfoVO.setProductList(currentClassificationCommercialSpecification);

            //加入list
            list.add(productClassificationIncludeProductInfoVO);
        }
        return new PageBean(allProductClassification.size(),list);
    }
}
